#!/bin/bash
# 串口问题诊断脚本

echo "=== CH9344 串口诊断工具 ==="
echo ""

GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'

echo "1️⃣ USB设备检测"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
if lsusb | grep -q "1a86"; then
    echo -e "${GREEN}✓${NC} 找到WCH USB设备:"
    lsusb | grep "1a86"
    PRODUCT_ID=$(lsusb | grep "1a86" | grep -o "1a86:[0-9a-f]*" | cut -d: -f2)
    echo "  产品ID: $PRODUCT_ID"
else
    echo -e "${RED}✗${NC} 未找到WCH USB设备 (1a86:xxxx)"
    echo "  请检查USB转串口设备是否已连接"
fi
echo ""

echo "2️⃣ 驱动模块检查"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
if lsmod | grep -q "ch9344"; then
    echo -e "${GREEN}✓${NC} ch9344 驱动已加载"
    lsmod | grep ch9344
elif lsmod | grep -q "ch341"; then
    echo -e "${YELLOW}⚠${NC} 使用的是 ch341 驱动 (兼容模式)"
    lsmod | grep ch341
    echo "  注意: 设备会显示为 /dev/ttyUSB* 而不是 /dev/ttyCH9344USB*"
else
    echo -e "${RED}✗${NC} 未加载任何CH9344/CH341驱动"
    echo "  需要安装并加载驱动"
    echo ""
    echo "  快速修复:"
    echo "    sudo modprobe ch341    # 临时使用兼容驱动"
    echo "  或"
    echo "    # 安装CH9344官方驱动 (参考SERIAL_SETUP_GUIDE.md)"
fi
echo ""

echo "3️⃣ 串口设备节点"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
FOUND_DEVICES=0

if ls /dev/ttyCH9344USB* >/dev/null 2>&1; then
    echo -e "${GREEN}✓${NC} CH9344 设备 (推荐):"
    ls -l /dev/ttyCH9344USB* | awk '{printf "  %-20s %s:%s  权限:%s\n", $NF, $3, $4, $1}'
    FOUND_DEVICES=1
fi

if ls /dev/ttyUSB* >/dev/null 2>&1; then
    echo -e "${YELLOW}⚠${NC} USB串口设备 (CH341兼容模式):"
    ls -l /dev/ttyUSB* | awk '{printf "  %-20s %s:%s  权限:%s\n", $NF, $3, $4, $1}'
    FOUND_DEVICES=1
fi

if ls /dev/ttyACM* >/dev/null 2>&1; then
    echo -e "${YELLOW}ℹ${NC} ACM串口设备:"
    ls -l /dev/ttyACM* | awk '{printf "  %-20s %s:%s  权限:%s\n", $NF, $3, $4, $1}'
    FOUND_DEVICES=1
fi

if [ $FOUND_DEVICES -eq 0 ]; then
    echo -e "${RED}✗${NC} 未找到任何串口设备"
    echo "  可能原因:"
    echo "    1. 驱动未加载"
    echo "    2. USB设备未正确连接"
    echo "    3. 设备识别失败"
fi
echo ""

echo "4️⃣ 用户权限"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
if groups | grep -q "dialout"; then
    echo -e "${GREEN}✓${NC} 当前用户在 dialout 组中"
    echo "  用户组: $(groups)"
else
    echo -e "${RED}✗${NC} 当前用户不在 dialout 组中"
    echo "  需要添加: sudo usermod -a -G dialout $USER"
    echo "  然后重新登录或运行: newgrp dialout"
fi
echo ""

echo "5️⃣ udev规则"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
if [ -f "/etc/udev/rules.d/99-ch9344.rules" ]; then
    echo -e "${GREEN}✓${NC} udev规则已安装"
    echo "  文件: /etc/udev/rules.d/99-ch9344.rules"
    if grep -q "55d9" /etc/udev/rules.d/99-ch9344.rules; then
        echo -e "  ${GREEN}✓${NC} 包含55d9产品ID支持"
    else
        echo -e "  ${YELLOW}⚠${NC} 未包含55d9产品ID,可能需要更新规则"
    fi
else
    echo -e "${YELLOW}⚠${NC} udev规则未安装"
    echo "  运行: sudo bash scripts/setup_serial_permissions.sh"
fi
echo ""

echo "6️⃣ 权限测试"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
TEST_DEVICE=""
if ls /dev/ttyCH9344USB0 >/dev/null 2>&1; then
    TEST_DEVICE="/dev/ttyCH9344USB0"
elif ls /dev/ttyUSB0 >/dev/null 2>&1; then
    TEST_DEVICE="/dev/ttyUSB0"
fi

if [ -n "$TEST_DEVICE" ]; then
    echo "  测试设备: $TEST_DEVICE"
    if [ -r "$TEST_DEVICE" ] && [ -w "$TEST_DEVICE" ]; then
        echo -e "  ${GREEN}✓${NC} 设备可读写"
    else
        echo -e "  ${RED}✗${NC} 设备权限不足"
        echo "    当前权限: $(ls -l $TEST_DEVICE | awk '{print $1, $3":"$4}')"
        echo "    需要权限: crw-rw-rw- root:dialout"
    fi
else
    echo -e "  ${YELLOW}⚠${NC} 无可用设备进行测试"
fi
echo ""

echo "7️⃣ 配置文件检查"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
if [ -f "/etc/modules" ]; then
    if grep -q "ch9344" /etc/modules; then
        echo -e "${GREEN}✓${NC} ch9344已配置为开机自动加载"
    elif grep -q "ch341" /etc/modules; then
        echo -e "${YELLOW}⚠${NC} ch341已配置为开机自动加载 (兼容模式)"
    else
        echo -e "${YELLOW}⚠${NC} 未配置开机自动加载"
        echo "  运行: echo 'ch9344' | sudo tee -a /etc/modules"
    fi
fi
echo ""

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📋 诊断总结"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

if [ $FOUND_DEVICES -eq 1 ] && groups | grep -q "dialout"; then
    echo -e "${GREEN}✅ 串口设备可用,应该可以正常使用${NC}"
    echo ""
    echo "运行应用程序:"
    echo "  cd ~/XiLi/orangepi"
    echo "  ./orangepi_gateway"
elif [ $FOUND_DEVICES -eq 0 ]; then
    echo -e "${RED}❌ 未找到串口设备 - 需要安装驱动${NC}"
    echo ""
    echo "解决步骤:"
    echo "  1. 阅读详细指南: cat SERIAL_SETUP_GUIDE.md"
    echo "  2. 安装CH9344驱动 (参考指南中的'方案1')"
    echo "  3. 或临时使用: sudo modprobe ch341"
    echo "  4. 重新运行此诊断脚本"
else
    echo -e "${YELLOW}⚠️  串口设备存在但可能有权限问题${NC}"
    echo ""
    echo "解决步骤:"
    echo "  1. 运行权限设置脚本:"
    echo "     sudo bash scripts/setup_serial_permissions.sh"
    echo "  2. 重新登录或运行: newgrp dialout"
    echo "  3. 重新运行此诊断脚本"
fi
echo ""
